local sk__qianji = fk.CreateSkill {

  name = "sk__qianji",

  tags = {},

}



sk__qianji:addEffect("viewas", {
  name = "sk__qianji",
  anim_type = "offensive",
  prompt = "#sk__qianji",
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    local c = Fk:cloneCard("slash")
    c.skillName = sk__qianji.name
    return c
  end,
  before_use = function(self, player, use)
    use.extraUse = true
    player.room:removePlayerMark(player, "@sk__qianji", 1)
  end,
  enabled_at_play = function(self, player)
    return player:getMark("@sk__qianji") > 0 and player.phase == Player.Play
  end,
  enabled_at_response = Util.FalseFunc,
})

sk__qianji:addEffect(fk.GameStart, {
  name = "#sk__qianji_trigger",
  mute = true,
  --sk__qianji,
  events = { fk.GameStart, fk.AfterCardsMove },
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__qianji.name) then
      if event == fk.GameStart then
        return true
      else
        for _, move in ipairs(data) do
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerEquip and (Fk:getCardById(info.cardId).sub_type == Card.SubtypeDefensiveRide or
                  Fk:getCardById(info.cardId).sub_type == Card.SubtypeOffensiveRide) then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("sk__qianji")
    if event == fk.GameStart then
      room:notifySkillInvoked(player, "sk__qianji", "drawcard")
      local cards = table.filter(room.draw_pile, function(id)
        local card = Fk:getCardById(id)
        return card.sub_type == Card.SubtypeDefensiveRide and player:canUse(card) and not player:prohibitUse(card)
      end)
      if #cards > 0 then
        room:useCard {
          from = player,
          card = Fk:getCardById(table.random(cards)),
        }
      end
      if player.dead then return end
      cards = table.filter(room.draw_pile, function(id)
        local card = Fk:getCardById(id)
        return card.sub_type == Card.SubtypeOffensiveRide and player:canUse(card) and not player:prohibitUse(card)
      end)
      if #cards > 0 then
        room:useCard {
          from = player,
          card = Fk:getCardById(table.random(cards)),
        }
      end
    else
      room:notifySkillInvoked(player, "sk__qianji", "special")
      room:addPlayerMark(player, "@sk__qianji", 2)
    end
  end,
})
sk__qianji:addEffect(fk.AfterCardsMove, {
  name = "#sk__qianji_trigger",
  mute = true,
  --sk__qianji,
  events = { fk.GameStart, fk.AfterCardsMove },
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__qianji.name) then
      if event == fk.GameStart then
        return true
      else
        for _, move in ipairs(data) do
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerEquip and (Fk:getCardById(info.cardId).sub_type == Card.SubtypeDefensiveRide or
                  Fk:getCardById(info.cardId).sub_type == Card.SubtypeOffensiveRide) then
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("sk__qianji")
    room:notifySkillInvoked(player, "sk__qianji", "special")
    room:addPlayerMark(player, "@sk__qianji", 2)
  end,
})

sk__qianji:addEffect("targetmod", {
  name = "#sk__qianji_targetmod",
  bypass_times = function(self, player, skill, scope, card, to)
    return card and type(card) == "table" and table.contains(card.skillNames, "sk__qianji")
  end,
  bypass_distances = function(self, player, skill, card, to)
    return card and type(card) == "table" and table.contains(card.skillNames, "sk__qianji")
  end,
})

return sk__qianji
